package com.pkutech.carcasher.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by cyy on 2016/6/10.
 */
public class CommonUtils {

    public static String getgetVersionName(Context context){
        PackageManager pm = context.getPackageManager();
        PackageInfo pi = null;
        try {
            pi = pm.getPackageInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return pi != null ? pi.versionName : "";
    }

    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return format.format(date);
    }

    public static String getDate(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }
    public static Date parseDateFromString(String dataStr) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.parse(dataStr);
    }

    public static Date getYesterdayDate(){
        Calendar  cal  = Calendar.getInstance();
        cal.add(Calendar.DATE,   -1);
        return cal.getTime();
    }

    public static Date parseDateFromString2(String dataStr) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.parse(dataStr);
    }
    public static int calAllPageCount(int dataSize){
        if(dataSize % 60 == 0){
            return dataSize / 60;
        }else{
            return dataSize / 60 + 1;
        }
    }

    public static byte[] readBytes(Context context, Uri uri) throws IOException {
        // this dynamically extends to take the bytes you read
        ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
        try {
            InputStream inputStream = context.getContentResolver().openInputStream(uri);

            // this is storage overwritten on each iteration with bytes
            int bufferSize = 1024;
            byte[] buffer = new byte[bufferSize];

            // we need to know how may bytes were read to write them to the byteBuffer
            int len = 0;
            while ((len = inputStream.read(buffer)) != -1) {
                byteBuffer.write(buffer, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // and then we can return your byte array.
        return byteBuffer.toByteArray();
    }

    public static String getSdcardDir() {
        if (Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
            return Environment.getExternalStorageDirectory().toString();
        }
        return null;
    }


    public static long getIntervalMinToNowTime(Date date){
        Date currentDate = new Date();
        long minute = (currentDate.getTime() - date.getTime())/(1000*60);
        return minute;
    }

    /**
     * @param date
     * @return
     * 验证该日期时当前年
     */
    public static boolean isCurrentYear(Date date){
        Calendar c = Calendar.getInstance();
        int currentYear = c.get(Calendar.YEAR);
        c.setTime(date);
        int localYear = c.get(Calendar.YEAR);
        return (currentYear == localYear);
    }

    public static String getPublishTimeString(Date date) {
        String timeString = "";
        long minute = CommonUtils.getIntervalMinToNowTime(date);
        if (minute == 0) {
            timeString = "刚刚";
            return timeString;
        } else if (minute > 0 && minute < 60) {
            timeString = String.valueOf(minute) + "分钟前";
            return timeString;
        } else {
            if (minute / 60 < 12) {
                timeString = String.valueOf(minute / 60) + "小时前";
                return timeString;
            } else {
                SimpleDateFormat sf = null;
                if (CommonUtils.isCurrentYear(date)) {
                    sf = new SimpleDateFormat("MM-dd HH:mm");
                } else {
                    sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                }
                timeString = sf.format(date);
                return timeString;
            }
        }
    }
}
